using Gj;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Transforms;

[UpdateInGroup(typeof(LateSimulationSystemGroup))]
public partial class AnimationDataSystem : SystemBase
{
    private EntityQuery translationGroup;

    protected override void OnCreate()
    {
        translationGroup = GetEntityQuery(new EntityQueryDesc
        {
            All = new[]
            {
                ComponentType.ReadOnly<AnimationEntityData>(),
                ComponentType.ReadWrite<AnimationTranslationData>()
            }
        });
    }

    // OnUpdate runs on the main thread.
    protected override void OnUpdate()
    {
        var inputDeps = Dependency;
        var EntityType = GetEntityTypeHandle();
        var animationEntityDataType = GetComponentTypeHandle<AnimationEntityData>();
        var animationTranslationDataType = GetComponentTypeHandle<AnimationTranslationData>();
        var localToWorldData = GetComponentLookup<LocalToWorld>();
        inputDeps = new TranslationJob
        {
            localToWorldData = localToWorldData
        }.Schedule(translationGroup, inputDeps);

        inputDeps.Complete();
    }

    [BurstCompile]
    private partial struct TranslationJob : IJobEntity
    {
        [ReadOnly] public ComponentLookup<LocalToWorld> localToWorldData;

        public void Execute(in AnimationEntityData animationEntityData,
            ref AnimationTranslationData animationTranslationData)
        {
            animationTranslationData.start = localToWorldData[animationEntityData.start].Position;
            animationTranslationData.end = localToWorldData[animationEntityData.end].Position;
        }
    }
}